// pages/geren/jingxixinlv/jingxixinlv.js

var utilBT = require('../../../js/utilBT.js');
var utilH = require('../../../js/utilHeart.js');

const app = getApp()
var hearts = []
Page({

    /**
     * 页面的初始数据
     */
    data: {
        color: ['#F1435C', '#4BAC51', '#03CE96', '#07957C', '#FAB659', '#FD764D'],
        start: false,
        heart_bpm: '--',
        heart_time: '----',
        conn: false,


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            conn: app.globalData.equconn0
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

        this.canvas0Draw(1, '---')

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // this.receiveData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        wx.offBLECharacteristicValueChange(function (res) {
            console.log('====>>取消监听蓝牙数据')
            console.log(res)
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    canvas0Draw: function (index, heart, msg) {

        var that = this

        const query = wx.createSelectorQuery().select('#canvas2')
            .fields({node: true, size: true})
            .exec((res) => {
                // console.log(res)
                const canvas = res[0].node

                const ctx = canvas.getContext('2d')

                canvas.width = 1000;
                canvas.height = 1000;
                ctx.clearRect(0, 0, 1000, 1000);


                ctx.beginPath()
                let img = canvas.createImage()
                img.src = '/img/xl-xin.png';

                img.onload = () => {
                    ctx.drawImage(img, 560, 370, 60, 60)
                }

                ctx.font = '140px Defult'
                ctx.fillStyle = 'black'
                ctx.textAlign = 'end'
                ctx.fillText(heart, 530, 480)

                ctx.font = '50px Defult'
                ctx.fillStyle = 'black'
                ctx.textAlign = 'start'
                ctx.fillText('BPM', 560, 480)

                ctx.font = '40px Georgia'
                ctx.fillStyle = 'black'
                ctx.textAlign = 'center'
                ctx.fillText('测量静息心率', 500, 550)

                ctx.closePath()

                for (let i = 0; i < 6; i++) {
                    let x = 500
                    let y = 500
                    let r = 330
                    let w = 60
                    if (index == i) {
                        r = 310
                        w = 100
                    }
                    ctx.beginPath()
                    ctx.arc(x, y, r, (i / 3 + 0.005) * Math.PI, ((i + 1) / 3 - 0.005) * Math.PI, false)
                    ctx.strokeStyle = that.data.color[i]
                    // ctx.fillStyle = that.data.color[i]
                    ctx.lineWidth = w
                    ctx.stroke()
                    ctx.closePath()

                }


            })
    },


    saveHR: function () {
        console.log('====>>saveHR')

    },
    receiveData: function () {
        var that = this
        wx.onBLECharacteristicValueChange(function (res) {
            console.log('====>>' + app.globalData.equ.name + '接收到的数据')
            var data = new Uint8Array(res.value)
            console.log(JSON.stringify(data))
            console.log('==>>心率：' + data[1])
            // if (data[1] < 25) {
            //     console.log('====>>心率太低')
            //     utilBT.closeconn(app.globalData.equ.deviceId)
            //     // that.canvas0Draw(1, '---', '未连接到设备')
            // } else if (data[1] > 0) {
                utilH.getIndex0(function (res) {
                    that.canvas0Draw(res[0], data[1], res[1])
                })
                hearts[hearts.length] = data[1]

            // }

        })
    },
    startTest: function () {
        hearts = []
        var that = this
        that.setData({
            start: true
        })
        that.receiveData()
        let heart = 0
        setTimeout(function () {
            wx.offBLECharacteristicValueChange(function (res) {
                console.log('====>>取消监听蓝牙数据')
                console.log(res)
            })

            heart = utilH.average(hearts)
            utilH.setRestHeart(heart)
            that.setData({
                heart_bpm: heart,
                start: false,
                heart_time: utilBT.formatTime(new Date())
            })

            that.canvas0Draw(1, '---')
        }, 1000 * 60)

    }

})
